package com.zeroup.auto.assists.step.unify;

import android.graphics.Rect;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.ven.assists.Assists;
import com.ven.assists.stepper.Step;
import com.ven.assists.stepper.StepCollector;
import com.zeroup.auto.assists.DouYinBaseStep;
import com.zeroup.auto.assists.kt.MyContinuation;
import com.zeroup.auto.assists.next.StepBeanData;
import com.zeroup.auto.utils.AssistsUtils;

import java.util.List;

import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.jvm.functions.Function1;

public class DoubleClickStep extends DouYinBaseStep {
    @Override
    public void onImpl(@NonNull StepCollector stepCollector) {
        stepCollector.next(1,false,(i,c)->{
            Object serviceData = AssistsUtils.getServiceData(i.getData());
            if(serviceData==null){
                return AssistsUtils.nextStep( i.getData());
            }
            List<AccessibilityNodeInfo> infoList = null;
            if(serviceData instanceof StepBeanData){
                StepBeanData stepBeanData = (StepBeanData)serviceData;
                if(stepBeanData.getSeachType()==1){
                    infoList = Assists.INSTANCE.findById(stepBeanData.getControlId());
                }else if(stepBeanData.getSeachType()==2){
                    infoList = Assists.INSTANCE.findByText(stepBeanData.getControlTxt());
                }else if(stepBeanData.getSeachType()==3){
                    infoList = Assists.INSTANCE.findByTags(stepBeanData.getControlTxt());
                }
                if(infoList==null || infoList.isEmpty()){
                    Log.d("DoubleClickStep===>error:","执行了双击事件，控件找不到");
                    return  Step.Companion.getNone();
                }
                if(stepBeanData.getSelectControlIndex()> infoList.size()){
                    Log.d("DoubleClickStep===>error:","执行了双击事件，索引溢出");
                    return  Step.Companion.getNone();
                }
                AccessibilityNodeInfo accessibilityNodeInfo = null;
                if(stepBeanData.getSelectControlIndex()==-1){//不指定确定的索引
                    accessibilityNodeInfo = infoList.stream().filter(g->{return g.isClickable();}).findFirst().get();
                }else{//确定第几个控件
                    accessibilityNodeInfo = infoList.get(stepBeanData.getSelectControlIndex());
                }
                if(accessibilityNodeInfo==null){
                    Log.d("DoubleClickStep===>error:","执行了双击事件，控件找不到");
                    return  Step.Companion.getNone();
                }
                if(!accessibilityNodeInfo.isClickable()){//如果不能点击，找父类第一个可以点击的控件点击
                    accessibilityNodeInfo = Assists.INSTANCE.findFirstParentClickable(accessibilityNodeInfo);
                }
                if(accessibilityNodeInfo==null){
                    accessibilityNodeInfo = infoList.get(stepBeanData.getSelectControlIndex());
                }
                if(accessibilityNodeInfo!=null){
                    if(accessibilityNodeInfo.isClickable()){
                        accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        Log.d("DoubleClickStep===>","执行了双击事件");
                    }else{
                        Rect rect = new Rect();
                        accessibilityNodeInfo.getBoundsInScreen(rect);
                        MyContinuation myContinuation = new MyContinuation();
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100,myContinuation);
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, myContinuation);
                    }
                }else{
                    Log.d("DoubleClickStep===>error:","执行双击事件，但是未找到可以点击的控件.");
                    return  Step.Companion.getNone();
                }
            }
            return AssistsUtils.nextStep(i.getData());
        });
    }
}
